package com.atguigu.servlet;

import com.atguigu.bean.Book;
import com.atguigu.bean.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ Author:任留辉
 * @ Description:
 * @ Date:Created in 21:19 2021/3/18 0018
 */
public class BookClientServlet extends BaseServlet {
private BookService bookService = new BookServiceImpl();
    /**
     * 处理客户端图书列表信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数，即访问的当前页的页码
        String pageNoStr = request.getParameter("pageNo");
       //获取价格区间的最大值和最小值
        String min = request.getParameter("min");
        String max = request.getParameter("max");
        //处理价格区间的数据
        int minPrice =0;
        int maxPrice =Integer.MAX_VALUE;
        try {
            minPrice = Integer.parseInt(min);
        } catch (NumberFormatException e) { }
        try {
            maxPrice = Integer.parseInt(max);
        } catch (NumberFormatException e) { }

        //创建page对象
        Page<Book> page = new Page<>();
        //为Page对象的属性赋值
        page.setPageNoStr(pageNoStr);
        page.setPageSize(10);
        //访问service处理业务逻辑，获取完整的page对象
       // page = bookService.getPage(page);
        page = bookService.getPageByPrice(page,minPrice,maxPrice);
        //将封装了分页相关的数据的page对象在域对象request中共享
        request.setAttribute("page", page);

        //转发到book_manager.jsp
        request.getRequestDispatcher("/pages/client/book_client.jsp").forward(request, response);
    }
}
